package ch.control;


import ch.service.impl.fserviceimpl;
import cn.hutool.core.io.FileUtil;
import ch.entity.result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("file")
public class filecontrol {
    @Autowired
    private fserviceimpl fserviceimpl;
//    private static final String rootpath="F:\\study\\files";
    private static final String rootpath=System.getProperty("user.dir")+"\\src\\main\\resources\\static\\files";
    @PostMapping("test")
    public result gettest(){
        return result.success("aaaaaaaaaaaa");
    }
    @GetMapping("getfilenames/{name}")
    public result getfilenames(@PathVariable String name){
        List getfilename = fserviceimpl.getfilename(name);
        if (getfilename.isEmpty()){
            return result.err("没有数据");
        }
        else {
            return result.success(getfilename);
        }
    }
    @PostMapping("upload/{filename}/{name}")
    public result upload(@RequestParam MultipartFile file, @PathVariable String filename,@PathVariable String name) throws IOException {
        String originalFilename = filename;
        String mainName = FileUtil.mainName(originalFilename);
        String extName = FileUtil.extName(originalFilename);
        System.out.println(mainName);
        System.out.println("qaaaaaaaaaaaaaaaa");
        System.out.println(filename);
        System.out.println(extName);
        System.out.println(name);
        String fileName=originalFilename;
        System.out.println(rootpath);
        if (!FileUtil.exist(rootpath)){
            FileUtil.mkdir(rootpath);
        }
        if (FileUtil.exist(rootpath+"\\"+fileName)){
            fileName=System.currentTimeMillis()+"_"+fileName;
        }
        boolean setfilename = fserviceimpl.setfilename(name,fileName);
        System.out.println(setfilename);

        if (!setfilename){
            System.out.println(setfilename);
            return result.err("数据库上转错误");
        }
        File savefile=new File(rootpath+"\\"+fileName);
        file.transferTo(savefile);
        String url="http://localhost:8081/file/download/"+fileName;
        return result.success(url);
    }

    @PostMapping("usuupload")
    public result upload(@RequestParam MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String mainName = FileUtil.mainName(originalFilename);
        String extName = FileUtil.extName(originalFilename);
        String fileName=originalFilename;
        System.out.println(rootpath);
        if (!FileUtil.exist(rootpath)){
            FileUtil.mkdir(rootpath);
        }
        if (FileUtil.exist(rootpath+"\\"+fileName)){
            fileName=System.currentTimeMillis()+"_"+fileName;
        }
        File savefile=new File(rootpath+"\\"+fileName);
        file.transferTo(savefile);
        String url="http://localhost:8081/file/download/"+fileName;
        return result.success(url);
    }


    @GetMapping("download/{fileName}")
    public void download(@PathVariable String fileName,
                         HttpServletResponse response) throws IOException {
        String filepath=rootpath+"\\"+fileName;
        System.out.println(filepath);
        if (!FileUtil.exist(filepath)){
            return;
        }
        byte[] bytes = FileUtil.readBytes(filepath);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }
    @GetMapping("getfile/{name}")
    public result getfile(@PathVariable String name){
        return result.success(fserviceimpl.getfile(name));
    }

}
